package com.fresh.controller;

import com.alibaba.excel.EasyExcel;
import com.fresh.client.GoodsFeignClient;
import com.fresh.common.ResponseResult;
import com.fresh.dto.ImportGoods;
import com.fresh.entity.FreshGoods;
import com.fresh.service.ExcelService;
import com.fresh.util.DemoDataListener;
import com.fresh.util.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List;

@RestController
@RequestMapping("/excel")
public class ExcelController {

    @Autowired
    private GoodsFeignClient goodsFeignClient;

    @Autowired
    private ExcelUtil excelUtil;

    @Autowired
    private ExcelService excelService;

    @GetMapping("/test")
    public ResponseResult test() {
        System.out.println("测试成功");
        ResponseResult result = goodsFeignClient.all();
        return ResponseResult.success(result.getData());
    }


    @GetMapping("/export/{type}")
    public ResponseResult download(HttpServletResponse response, @PathVariable String type) {
        try {
            String fileName = "导出数据表" + System.currentTimeMillis() + ".xlsx";
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
            if (type.equals("FreshGoods")) {
                ResponseResult result = goodsFeignClient.all();
                Object data = result.getData();
                List<FreshGoods> freshGoodsList = ExcelUtil.convertObjectToList(data, FreshGoods.class);
                System.out.println(freshGoodsList);
                EasyExcel.write(response.getOutputStream())
                        .head(FreshGoods.class)
                        .sheet(fileName)
                        .doWrite(freshGoodsList);

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseResult.success();
    }

    @PostMapping("/import/{type}")
    public ResponseResult import1(MultipartFile file, @PathVariable String type) {
        try {
            if (type.equals("FreshGoods")) {
                ImportGoods importGoods = excelUtil.convertTypeToObject(ImportGoods.class);
                EasyExcel.read(file.getInputStream(), importGoods.getClass(), new DemoDataListener(excelService))
                        .sheet()
                        .doRead();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseResult.success();
    }

    @PostMapping("/import/test")
    public ResponseResult import1(){
        FreshGoods freshGoods = new FreshGoods();
        goodsFeignClient.add(freshGoods);
        return ResponseResult.success();
    }


}
